package com.itcast.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itcast.entity.PageResult;
import com.itcast.entity.QueryPageBean;
import com.itcast.exception.PermissionException;
import com.itcast.mapper.PermissionMapper;
import com.itcast.pojo.Permission;
import com.itcast.service.PermissionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Set;


@Service(interfaceClass = PermissionService.class)
@Transactional
public class PermissionServiceImpl implements PermissionService{

    @Autowired
    PermissionMapper permissionMapper;

    @Override
    public void add(Permission permission) {
        permissionMapper.add(permission);
    }

    @Override
    public PageResult findPage(QueryPageBean queryPageBean) {
       Page page = PageHelper.startPage(queryPageBean.getCurrentPage(),queryPageBean.getPageSize());
       List<Permission> permissions = permissionMapper.findPage(queryPageBean.getQueryString());
        return new PageResult(page.getTotal(),permissions);
    }

    @Override
    public void edit(Permission permission) {
        permissionMapper.edit(permission);
    }

    @Override
    public Permission query(Integer id) {
        return permissionMapper.query(id);
    }

    @Override
    public void delete(Integer id) throws PermissionException{
       Set<Permission> permissions = permissionMapper.findPermissionsByRoles(id);
       Integer count  = permissions.size();
       if (null != count && count > 0){
           throw new PermissionException("存在引用不能删除");
       }
       permissionMapper.delete(id);
    }
}
